package com.blb.productservice.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.common.entity.Product;
import com.blb.common.util.ResponseResult;
import com.blb.productservice.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@RefreshScope
@RestController
public class ProductController {

    @Autowired
    private IProductService productService;

    @Value("${server.port}")
    private Integer port;

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @GetMapping("/product-config")
    public ResponseResult<String> showConfig(){
        return ResponseResult.ok(driverClassName +"--" + url +"--" + username +"--" + password);
    }

    @SentinelResource(value = "/product/{id}",
            blockHandlerClass = HelloControllerBlockHandler.class,
            blockHandler = "getProductByIdBlockHandler",
            fallbackClass = HelloControllerFallback.class,
            fallback = "getProductByIdFallback")
    @GetMapping("/product/{id}")
    public ResponseResult<Product> getProductById(@PathVariable Long id){
//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        if(new Random().nextInt(2) == 0){
//            throw new RuntimeException("Error!!");
//        }
        Product product = productService.getById(id);
        product.setName(product.getName() + port);
        return ResponseResult.ok(product);
    }

    @GetMapping("/product")
    public ResponseResult<Product> getProductById2(@RequestParam Long id){
        Product product = productService.getById(id);
        product.setName(product.getName() + port);
        return ResponseResult.ok(product);
    }

    @PostMapping("/product")
    public ResponseResult<String> saveProduct(@RequestBody Product product){
        productService.save(product);
        return ResponseResult.ok("ok");
    }

    @PutMapping("/product")
    public ResponseResult<String> updateProduct(@RequestBody Product product){
        productService.updateById(product);
        return ResponseResult.ok("ok");
    }

    @DeleteMapping("/product/{id}")
    public ResponseResult<String> updateProduct(@PathVariable Long id){
        productService.removeById(id);
        return ResponseResult.ok("ok");
    }

    @GetMapping("/product/page")
    public ResponseResult<Page<Product>> getProductPage(@RequestParam Long current){
        Page<Product> page = productService.page(new Page<>(current, 10));
        return ResponseResult.ok(page);
    }
}
